﻿using SerialPortHelperLib;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XChipTool
{
    public class FunctionConfig
    {
        //function selection
        uint app_mode = 0;  //0:car    1:tvr
        uint ae_lfc_enable = 1;
        uint SCBL_size = 2;  //0:cvbs   1:720p    2:1080p
        uint PWM_En = 0;
        uint Watch_Dog_En = 1;
        uint PSRAM_EN = 1;
        uint auto_sync_en = 1;
        uint System_Tick_Enable = 0;
        uint IR_Cut_En = 0;
        uint Register_Mirror_Flip_En = 1;
        uint DAC_Make_Up_Enable = 1;
        uint ADC_En = 0;
        uint ADC_OSD_En = 0;
        uint TVE_Stream_On_En = 1;

        uint hsize_in = 1920;
        uint vsize_in = 1080;

        //AE_LFC
        uint vts_in = 0x465;
        uint add_exp_line = 0x455;
        uint[] snr_vts_addr_enable = { 1, 1, 0, 0 };
        uint[] snr_vts_addr = { 0x320c, 0x320d, 0, 0 };
        uint insert_frame_num_max = 3;
        uint VTS_write_order = 0;

        //PWM
        uint PWM_Pin_Sel = 0x4000;//bit14

        //Watch_Dog
        uint Watch_Dog_Time = 0xffffff;

        //PSRAM_Check
        uint PSRAM0_EN = 1;
        uint PSRAM1_EN = 1;
        uint Psram0_MR0_Value = 0x17;
        uint Psram1_MR0_Value = 0x17;

        //DCBL
        uint nDcbl_Section_Index = 0;
        uint nDcbl_Load_Image_Flag = 1;
        uint Display_Image_Enable = 0;
        uint nDcbl_Cur_Image_Index = 5;
        uint nDcbl_Auto_Display = 0;
        uint nDcbl_Auto_Dispaly_Frame_Num = 1;

        //AutoSync

        //SysTick
        long  nSysTickClock = 7425000000;

        //IR_Cut
        uint ir_off_time = 4950000;
        uint ir_on_time = 4950000;

        //GPIO_CBL
        uint CarBackLine_Mode = 0;
        uint CarBackLine_Level_Toggle = 0;
        uint CarBackLine_Pin_Sel = 22;//GPIO22

        //Register_Mirror_Flip
        uint[] Sensor_Mirror_Flip_Addr_Enable = { 1, 0, 0, 0, 0, 0, 0, 0 };//SC1336 镜像配置
        uint[] Sensor_Mirror_Flip_Addr = { 0x3221, 0, 0, 0, 0, 0, 0, 0 };
        uint[] Mirror_Off_Flip_Off_Value = { 0, 0, 0, 0, 0, 0, 0, 0 };
        uint[] Mirror_On_Flip_Off_Value = { 6, 0, 0, 0, 0, 0, 0, 0 };
        uint[] Mirror_Off_Flip_On_Value = { 0x60, 0, 0, 0, 0, 0, 0, 0 };
        uint[] Mirror_On_Flip_On_Value = { 0x66, 0, 0, 0, 0, 0, 0, 0 };

        //GPIO_Mirror_Flip
        uint Mirror_Pin_Sel = 15;//GPIO15
        uint Flip_Pin_Sel = 14;//GPIO14
        uint Mirror_Level_Toggle = 0;
        uint Flip_Level_Toggle = 0;

        //DAC_Make_Up
        uint DAC_Make_Up_Pin_Select = 22;//GPIO22

        //ADC
        uint adc_chg_mask = 0x3ab;

        //ADC_OSD
        uint adc_up_value = 0x170;
        uint adc_down_value = 0x250;
        uint adc_right_value = 0xfb;
        uint adc_left_value = 0x6c;
        uint adc_menu_value = 0x00;
        uint adc_range_value = 0x20;

        //TVE_Stream_On
        uint Global_Stream_On_Enable = 1;
        uint Stream_On_Delay_Enable = 1;
        uint Stream_On_Delay_Cnt = 10;

        public uint App_mode
        {
            get
            {
                return app_mode;
            }

            set
            {
                app_mode = value;
            }
        }

        public uint Ae_lfc_enable
        {
            get
            {
                return ae_lfc_enable;
            }

            set
            {
                ae_lfc_enable = value;
            }
        }

        public uint SCBL_size1
        {
            get
            {
                return SCBL_size;
            }

            set
            {
                SCBL_size = value;
            }
        }

        public uint PWM_En1
        {
            get
            {
                return PWM_En;
            }

            set
            {
                PWM_En = value;
            }
        }

        public uint Watch_Dog_En1
        {
            get
            {
                return Watch_Dog_En;
            }

            set
            {
                Watch_Dog_En = value;
            }
        }

        public uint PSRAM_EN1
        {
            get
            {
                return PSRAM_EN;
            }

            set
            {
                PSRAM_EN = value;
            }
        }

        public uint Auto_sync_en
        {
            get
            {
                return auto_sync_en;
            }

            set
            {
                auto_sync_en = value;
            }
        }

        public uint System_Tick_Enable1
        {
            get
            {
                return System_Tick_Enable;
            }

            set
            {
                System_Tick_Enable = value;
            }
        }

        public uint IR_Cut_En1
        {
            get
            {
                return IR_Cut_En;
            }

            set
            {
                IR_Cut_En = value;
            }
        }

        public uint Register_Mirror_Flip_En1
        {
            get
            {
                return Register_Mirror_Flip_En;
            }

            set
            {
                Register_Mirror_Flip_En = value;
            }
        }

        public uint DAC_Make_Up_Enable1
        {
            get
            {
                return DAC_Make_Up_Enable;
            }

            set
            {
                DAC_Make_Up_Enable = value;
            }
        }

        public uint ADC_En1
        {
            get
            {
                return ADC_En;
            }

            set
            {
                ADC_En = value;
            }
        }

        public uint ADC_OSD_En1
        {
            get
            {
                return ADC_OSD_En;
            }

            set
            {
                ADC_OSD_En = value;
            }
        }

        public uint TVE_Stream_On_En1
        {
            get
            {
                return TVE_Stream_On_En;
            }

            set
            {
                TVE_Stream_On_En = value;
            }
        }

        public uint Hsize_in
        {
            get
            {
                return hsize_in;
            }

            set
            {
                hsize_in = value;
            }
        }

        public uint Vsize_in
        {
            get
            {
                return vsize_in;
            }

            set
            {
                vsize_in = value;
            }
        }

        public uint Vts_in
        {
            get
            {
                return vts_in;
            }

            set
            {
                vts_in = value;
            }
        }

        public uint Add_exp_line
        {
            get
            {
                return add_exp_line;
            }

            set
            {
                add_exp_line = value;
            }
        }

        public uint[] Snr_vts_addr_enable
        {
            get
            {
                return snr_vts_addr_enable;
            }

            set
            {
                snr_vts_addr_enable = value;
            }
        }

        public uint[] Snr_vts_addr
        {
            get
            {
                return snr_vts_addr;
            }

            set
            {
                snr_vts_addr = value;
            }
        }

        public uint Insert_frame_num_max
        {
            get
            {
                return insert_frame_num_max;
            }

            set
            {
                insert_frame_num_max = value;
            }
        }

        public uint VTS_write_order1
        {
            get
            {
                return VTS_write_order;
            }

            set
            {
                VTS_write_order = value;
            }
        }

        public uint PWM_Pin_Sel1
        {
            get
            {
                return PWM_Pin_Sel;
            }

            set
            {
                PWM_Pin_Sel = value;
            }
        }

        public uint Watch_Dog_Time1
        {
            get
            {
                return Watch_Dog_Time;
            }

            set
            {
                Watch_Dog_Time = value;
            }
        }

        public uint PSRAM0_EN1
        {
            get
            {
                return PSRAM0_EN;
            }

            set
            {
                PSRAM0_EN = value;
            }
        }

        public uint PSRAM1_EN1
        {
            get
            {
                return PSRAM1_EN;
            }

            set
            {
                PSRAM1_EN = value;
            }
        }

        public uint Psram0_MR0_Value1
        {
            get
            {
                return Psram0_MR0_Value;
            }

            set
            {
                Psram0_MR0_Value = value;
            }
        }

        public uint Psram1_MR0_Value1
        {
            get
            {
                return Psram1_MR0_Value;
            }

            set
            {
                Psram1_MR0_Value = value;
            }
        }

        public uint NDcbl_Section_Index
        {
            get
            {
                return nDcbl_Section_Index;
            }

            set
            {
                nDcbl_Section_Index = value;
            }
        }

        public uint NDcbl_Load_Image_Flag
        {
            get
            {
                return nDcbl_Load_Image_Flag;
            }

            set
            {
                nDcbl_Load_Image_Flag = value;
            }
        }

        public uint Display_Image_Enable1
        {
            get
            {
                return Display_Image_Enable;
            }

            set
            {
                Display_Image_Enable = value;
            }
        }

        public uint NDcbl_Cur_Image_Index
        {
            get
            {
                return nDcbl_Cur_Image_Index;
            }

            set
            {
                nDcbl_Cur_Image_Index = value;
            }
        }

        public uint NDcbl_Auto_Display
        {
            get
            {
                return nDcbl_Auto_Display;
            }

            set
            {
                nDcbl_Auto_Display = value;
            }
        }

        public uint NDcbl_Auto_Dispaly_Frame_Num
        {
            get
            {
                return nDcbl_Auto_Dispaly_Frame_Num;
            }

            set
            {
                nDcbl_Auto_Dispaly_Frame_Num = value;
            }
        }

        public long NSysTickClock
        {
            get
            {
                return nSysTickClock;
            }

            set
            {
                nSysTickClock = value;
            }
        }

        public uint Ir_off_time
        {
            get
            {
                return ir_off_time;
            }

            set
            {
                ir_off_time = value;
            }
        }

        public uint Ir_on_time
        {
            get
            {
                return ir_on_time;
            }

            set
            {
                ir_on_time = value;
            }
        }

        public uint CarBackLine_Mode1
        {
            get
            {
                return CarBackLine_Mode;
            }

            set
            {
                CarBackLine_Mode = value;
            }
        }

        public uint CarBackLine_Level_Toggle1
        {
            get
            {
                return CarBackLine_Level_Toggle;
            }

            set
            {
                CarBackLine_Level_Toggle = value;
            }
        }

        public uint CarBackLine_Pin_Sel1
        {
            get
            {
                return CarBackLine_Pin_Sel;
            }

            set
            {
                CarBackLine_Pin_Sel = value;
            }
        }

        public uint[] Sensor_Mirror_Flip_Addr_Enable1
        {
            get
            {
                return Sensor_Mirror_Flip_Addr_Enable;
            }

            set
            {
                Sensor_Mirror_Flip_Addr_Enable = value;
            }
        }

        public uint[] Sensor_Mirror_Flip_Addr1
        {
            get
            {
                return Sensor_Mirror_Flip_Addr;
            }

            set
            {
                Sensor_Mirror_Flip_Addr = value;
            }
        }

        public uint[] Mirror_Off_Flip_Off_Value1
        {
            get
            {
                return Mirror_Off_Flip_Off_Value;
            }

            set
            {
                Mirror_Off_Flip_Off_Value = value;
            }
        }

        public uint[] Mirror_On_Flip_Off_Value1
        {
            get
            {
                return Mirror_On_Flip_Off_Value;
            }

            set
            {
                Mirror_On_Flip_Off_Value = value;
            }
        }

        public uint[] Mirror_Off_Flip_On_Value1
        {
            get
            {
                return Mirror_Off_Flip_On_Value;
            }

            set
            {
                Mirror_Off_Flip_On_Value = value;
            }
        }

        public uint[] Mirror_On_Flip_On_Value1
        {
            get
            {
                return Mirror_On_Flip_On_Value;
            }

            set
            {
                Mirror_On_Flip_On_Value = value;
            }
        }

        public uint Mirror_Pin_Sel1
        {
            get
            {
                return Mirror_Pin_Sel;
            }

            set
            {
                Mirror_Pin_Sel = value;
            }
        }

        public uint Flip_Pin_Sel1
        {
            get
            {
                return Flip_Pin_Sel;
            }

            set
            {
                Flip_Pin_Sel = value;
            }
        }

        public uint Mirror_Level_Toggle1
        {
            get
            {
                return Mirror_Level_Toggle;
            }

            set
            {
                Mirror_Level_Toggle = value;
            }
        }

        public uint Flip_Level_Toggle1
        {
            get
            {
                return Flip_Level_Toggle;
            }

            set
            {
                Flip_Level_Toggle = value;
            }
        }

        public uint DAC_Make_Up_Pin_Select1
        {
            get
            {
                return DAC_Make_Up_Pin_Select;
            }

            set
            {
                DAC_Make_Up_Pin_Select = value;
            }
        }

        public uint Adc_chg_mask
        {
            get
            {
                return adc_chg_mask;
            }

            set
            {
                adc_chg_mask = value;
            }
        }

        public uint Adc_up_value
        {
            get
            {
                return adc_up_value;
            }

            set
            {
                adc_up_value = value;
            }
        }

        public uint Adc_down_value
        {
            get
            {
                return adc_down_value;
            }

            set
            {
                adc_down_value = value;
            }
        }

        public uint Adc_right_value
        {
            get
            {
                return adc_right_value;
            }

            set
            {
                adc_right_value = value;
            }
        }

        public uint Adc_left_value
        {
            get
            {
                return adc_left_value;
            }

            set
            {
                adc_left_value = value;
            }
        }

        public uint Adc_menu_value
        {
            get
            {
                return adc_menu_value;
            }

            set
            {
                adc_menu_value = value;
            }
        }

        public uint Adc_range_value
        {
            get
            {
                return adc_range_value;
            }

            set
            {
                adc_range_value = value;
            }
        }

        public uint Global_Stream_On_Enable1
        {
            get
            {
                return Global_Stream_On_Enable;
            }

            set
            {
                Global_Stream_On_Enable = value;
            }
        }

        public uint Stream_On_Delay_Enable1
        {
            get
            {
                return Stream_On_Delay_Enable;
            }

            set
            {
                Stream_On_Delay_Enable = value;
            }
        }

        public uint Stream_On_Delay_Cnt1
        {
            get
            {
                return Stream_On_Delay_Cnt;
            }

            set
            {
                Stream_On_Delay_Cnt = value;
            }
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="sPortTemp">串口</param>
        /// <param name="isUsb">是否是USB设备</param>
        public void Initialization(SerialPort sPortTemp, bool isUsb = true)
        {

            //nr3d_enable = (SerialSend.ReadRegister(0x80300002, sPortTemp) & 0x8) == 0x8 ? 1 : 0;
            //isp_scale_sel = (SerialSend.ReadRegister(0x80300001, sPortTemp) & 0x10) == 0x10 ? 1 : 0;
            //afc_enable = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;

            //if (((SerialSend.ReadRegister(0x80300013, sPortTemp) & 0x80) == 0x80))
            //{
            //    hdr_mode = 2;
            //}
            //if (((SerialSend.ReadRegister(0x80300013, sPortTemp) & 0x40) == 0x40))
            //{
            //    hdr_mode = 3;
            //}
            //if ((SerialSend.ReadRegister(0x80300013, sPortTemp) == 0x00))
            //{
            //    hdr_mode = 0;
            //}

            ////mjpeg_enable = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;
            ////IQswitch_config = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;
            ////hsize_in  8030 1f04 1f05
            //hsize_in = Convert.ToUInt32($"{SerialSend.ReadRegister(0x80301f04, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x80301f05, sPortTemp).ToString("X2")}", 16);
            ////vsize_in 8030 1f06 1f07
            //vsize_in = Convert.ToUInt32($"{SerialSend.ReadRegister(0x80301f06, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x80301f07, sPortTemp).ToString("X2")}", 16);
            //// max_expo_line 0x200000c7 0x200000c6  0x200000c5  0x200000c4
            //max_expo_line = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000c7, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c6, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c5, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c4, sPortTemp).ToString("X2")}", 16);
            ////sensor_i2c_id 0x200000fe
            //sensor_i2c_id = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000fe, sPortTemp).ToString("X2")}", 16);
            ////sensor_i2c_mode 0x200000ff
            //sensor_i2c_mode = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000ff, sPortTemp).ToString("X2")}", 16);
            ////type_gain  0x20000100
            //type_gain = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000100, sPortTemp).ToString("X2")}", 16);
            ////type_exp  0x20000101
            //type_exp = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000101, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_addr0  0x20000128 0x20000129
            //sensor_gain_addr[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000128, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000129, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_addr1  0x2000012a 0x2000012b
            //sensor_gain_addr[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012a, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012b, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_addr2 0x2000012c  0x2000012d
            //sensor_gain_addr[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012c, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012d, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_addr3  0x2000012e 0x2000012f
            //sensor_gain_addr[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012e, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012f, sPortTemp).ToString("X2")}", 16);

            ////sensor_gain_mask0 0x20000130 0x20000131
            //sensor_gain_mask[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000130, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000131, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_mask1 0x20000132 0x20000133
            //sensor_gain_mask[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000132, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000133, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_mask2 0x20000134 0x20000135
            //sensor_gain_mask[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000134, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000135, sPortTemp).ToString("X2")}", 16);
            ////sensor_gain_mask3 0x20000136 0x20000137
            //sensor_gain_mask[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000136, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000137, sPortTemp).ToString("X2")}", 16);

            ////sensor_exp_addr0 0x20000108 0x20000109
            //sensor_exp_addr[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000108, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000109, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_addr1 0x2000010a 0x2000010b
            //sensor_exp_addr[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010a, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010b, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_addr2 0x2000010c 0x2000010d
            //sensor_exp_addr[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010c, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010d, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_addr3 0x2000010e 0x2000010f
            //sensor_exp_addr[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010e, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010f, sPortTemp).ToString("X2")}", 16);

            ////sensor_exp_mask0  0x20000110  0x20000111
            //sensor_exp_mask[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000110, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000111, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_mask1 0x20000112 0x20000113
            //sensor_exp_mask[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000112, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000113, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_mask2  0x20000114 0x20000115
            //sensor_exp_mask[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000114, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000115, sPortTemp).ToString("X2")}", 16);
            ////sensor_exp_mask3  0x20000116  0x20000117
            //sensor_exp_mask[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000116, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000117, sPortTemp).ToString("X2")}", 16);

            ////long2short_delay   0x20000257 0x20000256 0x20000255 0x20000254
            //long2short_delay = Convert.ToInt32($"{SerialSend.ReadRegister(0x20000257, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000256, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000255, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000254, sPortTemp).ToString("X2")}", 16);
            ////short2vs_delay  0x20000260  0x20000261 0x20000262 0x20000263
            //short2vs_delay = Convert.ToInt32($"{SerialSend.ReadRegister(0x20000260, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000261, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000262, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000263, sPortTemp).ToString("X2")}", 16);



        }
    }
}
